package com.kintreda.ieco_server.core.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

public class CustomDateFormat extends SimpleDateFormat {

	private static final long serialVersionUID = -6605713783491913867L;
	
	public final static String DATETIME_WEB = "yyyy-MM-dd HH:mm:ss.SSS";

	public final static String DATETIME_FULL = "yyyy-MM-dd HH:mm:ss:SSS";
	public final static String DATETIME_SECONDS = "yyyy-MM-dd HH:mm:ss";
	public final static String DATETIME_SIMPLE = "yyyy-MM-dd HH:mm";

	public final static String DATE_FULL = "yyyy-MM-dd";
	public final static String DATE_FULL_CLEAN = "yyyyMMdd";
	public final static String DATE_SIMPLE = "yyyy-MM";
	public final static String DATE_YEAR = "yyyy";

	public final static String TIME_FULL = "HH:mm:ss:SSS";
	public final static String TIME_SECONDS = "HH:mm:ss";
	public final static String TIME_SIMPLE = "HH:mm";

	public final static String[] patterns = new String[] { DATETIME_WEB,DATETIME_FULL,
			DATETIME_SECONDS, DATETIME_SIMPLE, DATE_FULL, DATE_FULL_CLEAN,
			DATE_SIMPLE, DATE_YEAR, TIME_FULL, TIME_SECONDS, TIME_SIMPLE };

	@Override
	public Date parse(String datetime) {
		if (StringUtils.isNotBlank(datetime)) {
			datetime = datetime.trim();
			try {
				return DateUtils.parseDate(datetime, patterns);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
